package com.zk5_education.service_edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zk5_education.service_edu.entity.EduSubject;
import com.zk5_education.service_edu.entity.excel.SubjectExcel;
import com.zk5_education.service_edu.entity.subject.OneSubject;
import com.zk5_education.service_edu.entity.subject.TwoSubject;
import com.zk5_education.service_edu.listeren.SubjectListener;
import com.zk5_education.service_edu.mapper.EduSubjectMapper;
import com.zk5_education.service_edu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-07
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    public void addsubject(MultipartFile file,EduSubjectService eduSubjectService) {

        try{
            InputStream in=file.getInputStream();
            EasyExcel.read(in, SubjectExcel.class,new SubjectListener(eduSubjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }


    }

    @Override
    public List<OneSubject> findAllsubject() {

        //查询所有的一级分类
        QueryWrapper<EduSubject>  OneQuery=new QueryWrapper<>();
        OneQuery.eq("parent_id",0);
        List<EduSubject> OneSubjects = baseMapper.selectList(OneQuery);
        //查询所有的二级分类
        QueryWrapper<EduSubject>  TneQuery=new QueryWrapper<>();
        OneQuery.ne("parent_id",0);
        List<EduSubject> TwoSubjects = baseMapper.selectList(TneQuery);
        List<OneSubject>list=new ArrayList<>();
        for (EduSubject on:OneSubjects) {
            OneSubject ot=new OneSubject();
            BeanUtils.copyProperties(on,ot);
           List<TwoSubject>list1=new ArrayList<>();
            for(EduSubject in:TwoSubjects){
                if(on.getId().equals(in.getParentId())){
                    TwoSubject tb=new TwoSubject();
                    BeanUtils.copyProperties(in,tb);
                    list1.add(tb);
                }
            }
            ot.setChildren(list1);
            list.add(ot);
        }
        return list;
    }
}
